package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.DeviceListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.model.DeviceListInfo;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.List;

public class MyDeviceListActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

	private List<DeviceListInfo> datas = new ArrayList<>();
	private DeviceListAdapter adapter;
	private PullToRefreshScrollView pullToRefreshScrollView;
	private MyListView listview;
	private int page = 1;
	private int rows = Config.ROWS;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_my_device_list);
		MyActivityManager.getInstance().addActivity(this);
		initView();
	}

	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "收银设备管理");
		showView(R.id.next_sure, false);
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);




		pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pullToRefreshScrollView);
		pullToRefreshScrollView.setOnRefreshListener(new MyDeviceListActivity.MyOnRefreshListener());
		pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
		listview = (MyListView)findViewById(R.id.id_device_list);
		listview.setOnItemClickListener(this);

		for (int i = 0; i < 10; i++){
			DeviceListInfo deviceListInfo = new DeviceListInfo();
			deviceListInfo.setName("黄金等级");
			deviceListInfo.setStatus(1);
			datas.add(deviceListInfo);
		}


		adapter = new DeviceListAdapter(this, datas);
		listview.setAdapter(adapter);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		startActivity(new Intent(mContext, MyDeviceItemDetailActivity.class));
	}


	class MyOnRefreshListener implements PullToRefreshBase.OnRefreshListener2<ScrollView> {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
			String label = DateUtils.formatDateTime(mContext, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
			refreshData();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			page++;
			getdata();
		}
	}



	private void refreshData(){
		datas.clear();
		page = 1;
		getdata();
	}

	private void getdata(){
//		Map<String, Object> params = new HashMap<String, Object>();
////		params.put("sid", mApp.getLoginEntity().getSid());
////		String key = getTvText(R.id.tv_search_key);
////		if(!StringUtil.isNull(key)){params.put("key", key);}
////		else{params.put("key", "");}
//		params.put("pagenum", page);
//		params.put("pagesize", rows);
//		API.http_post(this, API.COUPON_LIST, params, new HttpCallback<CouponListEntity>(this) {
//			@Override
//			public void onSuccess(int statusCode, CouponListEntity data) {
//				if(data!=null){
//					List<CouponEntity> tmp_datas = data.getCoupons();
//					if(tmp_datas==null||tmp_datas.size()==0||tmp_datas.size()<rows){
//						if(tmp_datas==null||tmp_datas.size()==0){
//							toast("没有更多数据");
//						}
//						pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
//					}
//					if(tmp_datas!=null&&tmp_datas.size()>0){
//						datas.addAll(tmp_datas);
//					}
//				}
//				boolean no_datas = (datas==null||datas.size()==0);
//				showView(R.id.eva_nodata, no_datas);
//				showView(R.id.listview, !no_datas);
//				adapter.notifyDataSetChanged();
//				pullToRefreshScrollView.onRefreshComplete();
//			}
//
//			@Override
//			public void onFailure(int code, String message) {
//				pullToRefreshScrollView.onRefreshComplete();
//				super.onFailure(code, message);
//
//			}
//
//			@SuppressWarnings({ "unchecked", "rawtypes" })
//			@Override
//			public TypeToken getTypeToken() {
//				return new TypeToken<CouponListEntity>() {
//				};
//			}
//		});
	}





	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;

			default:break;
		}
	}




}
